<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');


/*
  Reads an XML schema file and generates an interface with which to edit the
  data in the specified mysql table.
*/

define('PATH_TO_SCHEMAS', '/home/.kuce/michaelothomas/cs.michaelothomas.com/includes/schemas/');

define('PARAM_ACTION', 'action');
define('PARAM_ACTION_LIST', 'list');
define('PARAM_SCHEMA', 'schema');

require_once ('class_form.php');
require_once ('class_tablemaker.php');

class CSEdit {

  private $action;
  private $schemaFiles = array();
  private $validActions = array(PARAM_ACTION_LIST);
  
  private $schemaObject;

  /*
    Constructor.
  */
  function CSEdit() {
  
  }


  /*
    Determine the action to be taken from $_GET.
  */
  function Execute() {
    $this->ValidateInput();
    if ($this->action == PARAM_ACTION_LIST) {
      $this->ListSchemas();
    } else {
      $this->FindSchemaFiles();
      $this->DisplaySchemaList();
    }
  }

  function ValidateInput() {
    if (isset($_GET[PARAM_ACTION])) $this->action = $_GET[PARAM_ACTION];
    if (isset($_POST[PARAM_ACTION])) $this->action = $_POST[PARAM_ACTION];
    
    if ($this->action == PARAM_ACTION_LIST) {
      if (!isset($_GET[PARAM_SCHEMA])) {
        die ('No Schema File Specified');
      }
    }
  }

  
  /*
    Search the specified folder for XML schema files.
  */
  function FindSchemaFiles() {
    $dirlist = opendir(PATH_TO_SCHEMAS);
    while ($file = readdir($dirlist)) {
      $pieces = explode('.', $file);
      $fileType = $pieces[count($pieces) - 1];
      if (!is_dir(PATH_TO_SCHEMAS . $file) && ($fileType == 'xml')) {
        $this->schemaFiles[$file] = simplexml_load_file(PATH_TO_SCHEMAS . $file,
            null, LIBXML_DTDATTR | LIBXML_DTDVALID | LIBXML_DTDLOAD);
      }
    }
    closedir($dirlist);
  }
  

  /*
    The default action is to display a list of mysql tables which can be edited.
  */
  function DisplaySchemaList() {
    foreach ($this->schemaFiles as $filename => $xml) {
      $attributes = array();
      foreach ($xml->attributes() as $key => $value) {
        $attributes[$key] = strval($value);
      }
      
      if ($xml->getName() == 'csedit') {
        echo "<a href=\"?action=list&schema={$filename}\">{$attributes['title']}</a>\n";
      }
    
    }  
  }

  function ListSchemas() {
    $this->LoadXML();
    $table = new tablemaker($this->schemaObject['table']);
    $table->get_columns();
    $table->add_constraint("current = 1");
    $table->set_hidden_column('id');
    $table->set_hidden_column('current');
    $table->set_hidden_column('timestamp');
    foreach ($this->schemaObject['field'] as $field) {
      if ($field['listable'] !== 'true') {
        $table->set_hidden_column($field['name']);
      }
    }
    $table->add_button("?action=delete&schema={$_GET[PARAM_SCHEMA]}", 'user_id', 'Del', 'delete');
    $table->add_button("?action=edit&schema={$_GET[PARAM_SCHEMA]}", 'user_id', 'Edit', 'edit');
    $table->show_table();


    


   
  }

  function LoadXML() {
    $file = $_GET[PARAM_SCHEMA];
    $simpleXML = simplexml_load_file(PATH_TO_SCHEMAS . $file, null,
        LIBXML_DTDATTR | LIBXML_DTDVALID | LIBXML_DTDLOAD);
    $this->schemaObject = $this->SimpleXMLToArray($simpleXML);
  }


  function SimpleXMLToArray($xmlObj) {
    $array = array();
    foreach ($xmlObj->attributes() as $key => $value) {
      $array[$key] = strval($value);
    }
    foreach ($xmlObj->children() as $child) {
      if (!isset($array[$child->getName()])) {
        $array[$child->getName()] = array();
      }
      $array[$child->getName()][] = $this->SimpleXMLToArray($child);
    }
    if (count($array) > 0) return $array;
  }


}



?>
